Module modBillToClient

    Public Function Upload_BillToClient_FHG(ByVal oDv As DataView, ByVal bServiceItem As Boolean, ByVal bIsNonPanel As Boolean, ByRef sErrDesc As String) As Long

        Dim sFuncName As String = String.Empty
        Dim sCardName As String = String.Empty
        Dim sCardCode As String = String.Empty
        Dim sSQL As String = String.Empty
        Dim oRS As SAPbobsCOM.Recordset = Nothing
        Dim oDS As New DataSet
        Dim oDatasetBP As New DataSet
        Dim sBatchNo As String = String.Empty
        Dim sBatchPeriod As String = String.Empty
        Dim k, m As Integer
        Dim oBPArrary As New ArrayList
        Dim sBillType As String = String.Empty
        Dim oBPDT As DataTable

        Try
            sFuncName = "Upload_BillToClient_FHG"
            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Reading Worksheet data.", sFuncName)
            oRS = p_oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)

            sBatchNo = oDv(2)(0).ToString()
            sBatchPeriod = oDv(3)(0).ToString()

            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Reading Batch No.", sFuncName)
            k = InStrRev(sBatchNo, ":")
            sBatchNo = Microsoft.VisualBasic.Right(sBatchNo, Len(sBatchNo) - k).Trim
            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Batch No:" & sBatchNo, sFuncName)

            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Reading Batch Date.", sFuncName)
            m = InStrRev(sBatchPeriod, "to")
            sBatchPeriod = Microsoft.VisualBasic.Right(sBatchPeriod, Len(sBatchPeriod) - m - 1).Trim
            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Batch Date: " & sBatchPeriod, sFuncName)

            For i As Integer = 6 To oDv.Count - 1
                If Not oBPArrary.Contains(oDv(i)(2).ToString) Then
                    If Not oDv(i)(2).ToString = String.Empty Then
                        oBPArrary.Add(oDv(i)(2).ToString)
                    End If
                End If
            Next

            oBPDT = oDv.Table.Clone
            oBPDT.Clear()

            For iRow As Integer = 0 To oBPArrary.Count - 1

                sBillType = String.Empty
                '1. Get Billing Type from OCRD
                GetBillingType(oBPArrary(iRow).ToString, sBillType)
                Dim sCName As String = oBPArrary(iRow).ToString

                If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Getting BillType for ::" & sCName, sFuncName)

                If sBillType = String.Empty Then Throw New ArgumentException("No Billing Type specified for ::" & sCName)

                '2. SELECT BILLTYPE
                Dim BPRows() As DataRow = oDv.Table.Select("F3='" & oBPArrary(iRow).ToString & "'")
                oBPDT.Clear()
                For Each row As DataRow In BPRows
                    oBPDT.ImportRow(row)
                Next

                'Create A/R Invoice
                Select Case sBillType

                    Case "CO"
                        Console.WriteLine("Creating A/R Invoice")
                        sCardName = oBPArrary(iRow).ToString
                        If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Calling AddARInvoice_FHG()", sFuncName)
                        If AddARInvoice_FHG(sCardName, BPRows, sBatchNo, sBatchPeriod, bServiceItem, False, sErrDesc) <> RTN_SUCCESS Then
                            Throw New ArgumentException(sErrDesc)
                        End If

                    Case "ENT"
                        Dim oEnTArray As New ArrayList
                        For Each row As DataRow In BPRows
                            If Not oEnTArray.Contains(row.Item(5).ToString) Then oEnTArray.Add(row.Item(5).ToString)
                        Next

                        For iEntRow As Integer = 0 To oEnTArray.Count - 1
                            Console.WriteLine("Creating A/R Invoice")
                            Dim EntRows() As DataRow = oBPDT.Select("F6='" & oEnTArray(iEntRow).ToString & "'")
                            sCardName = oEnTArray(iEntRow).ToString

                            If EntRows.Length = 0 Then
                                sErrDesc = "Entity is blank for ::" & oBPArrary(iRow).ToString
                                Throw New ArgumentException(sErrDesc)
                            End If

                            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Calling AddARInvoice_FHG()", sFuncName)
                            If AddARInvoice_FHG(sCardName, EntRows, sBatchNo, sBatchPeriod, bServiceItem, False, sErrDesc) <> RTN_SUCCESS Then
                                Throw New ArgumentException(sErrDesc)
                            End If
                        Next

                    Case "CO-DEPT"
                        Dim oDepArray As New ArrayList
                        For Each row As DataRow In BPRows
                            If Not oDepArray.Contains(row.Item(6).ToString) Then oDepArray.Add(row.Item(6).ToString)
                        Next

                        For iDepRow As Integer = 0 To oDepArray.Count - 1
                            Console.WriteLine("Creating A/R Invoice")
                            Dim DepRows() As DataRow = oBPDT.Select("F7='" & oDepArray(iDepRow).ToString & "'")
                            sCardName = oBPArrary(iRow).ToString

                            If DepRows.Length = 0 Then
                                sErrDesc = "Department is blank for ::" & sCardName
                                Throw New ArgumentException(sErrDesc)
                            End If


                            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Calling AddARInvoice_FHG()", sFuncName)
                            If AddARInvoice_FHG(sCardName, DepRows, sBatchNo, sBatchPeriod, bServiceItem, False, sErrDesc) <> RTN_SUCCESS Then
                                Throw New ArgumentException(sErrDesc)
                            End If
                        Next

                    Case "ENT-DEPT"
                        Dim oEntDeptDT As DataTable
                        oEntDeptDT = oBPDT.DefaultView.ToTable(True, "F6", "F7")

                        For Each row As DataRow In oEntDeptDT.Rows
                            Console.WriteLine("Creating A/R Invoice")

                            Dim oEntDeptRows() As DataRow = oBPDT.Select("F6='" & row.Item(0).ToString & "' and F7='" & row.Item(1).ToString & "'")
                            sCardName = row.Item(0).ToString

                            If oEntDeptRows.Length = 0 Then
                                sErrDesc = "Entity/Department is blank for ::" & sCardName
                                Throw New ArgumentException(sErrDesc)
                            End If


                            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Calling AddARInvoice_FHG()", sFuncName)
                            If AddARInvoice_FHG(sCardName, oEntDeptRows, sBatchNo, sBatchPeriod, bServiceItem, False, sErrDesc) <> RTN_SUCCESS Then
                                Throw New ArgumentException(sErrDesc)
                            End If

                        Next

                    Case "CO-DEPT-CC"
                        Dim oDeptCCDT As DataTable
                        oDeptCCDT = oBPDT.DefaultView.ToTable(True, "F7", "F8")

                        For Each row As DataRow In oDeptCCDT.Rows
                            Console.WriteLine("Creating A/R Invoice")
                            Dim oDeptCCRows() As DataRow = oBPDT.Select("F7='" & row.Item(0).ToString & "' and F8='" & row.Item(1).ToString & "'")
                            sCardName = oBPArrary(iRow).ToString

                            If oDeptCCRows.Length = 0 Then
                                sErrDesc = "Department/CostCenter is blank for ::" & sCardName
                                Throw New ArgumentException(sErrDesc)
                            End If

                            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Calling AddARInvoice_FHG()", sFuncName)
                            If AddARInvoice_FHG(sCardName, oDeptCCRows, sBatchNo, sBatchPeriod, bServiceItem, False, sErrDesc) <> RTN_SUCCESS Then
                                Throw New ArgumentException(sErrDesc)
                            End If
                        Next

                    Case "ENT-DEPT-CC"

                        Dim oENTDEPCCDT As DataTable
                        oENTDEPCCDT = oBPDT.DefaultView.ToTable(True, "F6", "F7", "F8")

                        For Each row As DataRow In oENTDEPCCDT.Rows
                            Console.WriteLine("Creating A/R Invoice")
                            Dim oENTDEPCCRows() As DataRow = oBPDT.Select("F6='" & row.Item(0).ToString & "' and F7='" & row.Item(1).ToString & "' and F8='" & row.Item(2).ToString & "'")
                            sCardName = row.Item(0).ToString

                            If oENTDEPCCRows.Length = 0 Then
                                sErrDesc = "Entity/Department/CostCenter is blank for ::" & sCardName
                                Throw New ArgumentException(sErrDesc)
                            End If

                            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Calling AddARInvoice_FHG()", sFuncName)
                            If AddARInvoice_FHG(sCardName, oENTDEPCCRows, sBatchNo, sBatchPeriod, bServiceItem, False, sErrDesc) <> RTN_SUCCESS Then
                                Throw New ArgumentException(sErrDesc)
                            End If
                        Next

                    Case "CO-CC"
                        Dim oCCArray As New ArrayList
                        For Each row As DataRow In BPRows
                            If Not oCCArray.Contains(row.Item(7).ToString) Then
                                If Not row.Item(7).ToString = String.Empty Then
                                    oCCArray.Add(row.Item(7).ToString)
                                End If
                            End If
                        Next
                        For iCCRow As Integer = 0 To oCCArray.Count - 1
                            Console.WriteLine("Creating A/R Invoice")
                            Dim CCRows() As DataRow = oBPDT.Select("F8='" & oCCArray(iCCRow).ToString & "'")
                            sCardName = oBPArrary(iRow).ToString

                            If CCRows.Length = 0 Then
                                sErrDesc = "Cost Center is blank for ::" & sCardName
                                Throw New ArgumentException(sErrDesc)
                            End If

                            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Calling AddARInvoice_FHG()", sFuncName)
                            If AddARInvoice_FHG(sCardName, CCRows, sBatchNo, sBatchPeriod, bServiceItem, False, sErrDesc) <> RTN_SUCCESS Then
                                Throw New ArgumentException(sErrDesc)
                            End If
                        Next

                    Case "ENT-CC"
                        Dim oEntCCDT As DataTable
                        oEntCCDT = oBPDT.DefaultView.ToTable(True, "F6", "F8")

                        For Each row As DataRow In oEntCCDT.Rows
                            Console.WriteLine("Creating A/R Invoice")
                            Dim oEntCCRows() As DataRow = oBPDT.Select("F6='" & row.Item(0).ToString & "' and F8='" & row.Item(1).ToString & "'")
                            sCardName = row.Item(0).ToString

                            If oEntCCRows.Length = 0 Then
                                sErrDesc = "Entity/Cost center is blank for ::" & sCardName
                                Throw New ArgumentException(sErrDesc)
                            End If

                            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Calling AddARInvoice_FHG()", sFuncName)
                            If AddARInvoice_FHG(sCardName, oEntCCRows, sBatchNo, sBatchPeriod, bServiceItem, False, sErrDesc) <> RTN_SUCCESS Then
                                Throw New ArgumentException(sErrDesc)
                            End If
                        Next

                End Select
            Next


            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Complete Function with SUCCESS", sFuncName)
            Upload_BillToClient_FHG = RTN_SUCCESS

        Catch ex As Exception
            Upload_BillToClient_FHG = RTN_ERROR
            sErrDesc = ex.Message
            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile("Function completed with ERROR:" & sErrDesc, sFuncName)
        Finally

        End Try
    End Function

    Private Function AddARInvoice_FHG(ByVal sCardName As String, _
                                        ByVal oBPRow() As DataRow, _
                                        ByVal sBatchNo As String, _
                                        ByVal sBatchPeriod As String, _
                                        ByVal bServiceItem As Boolean, _
                                        ByVal bIsNonPanel As Boolean, _
                                        ByRef sErrDesc As String) As Long

        Dim sFuncName As String = String.Empty
        Dim sCardCode As String = String.Empty
        Dim oDoc As SAPbobsCOM.Documents
        Dim iCnt As Integer
        Dim lRetCode, lErrCode As Long
        Dim sRemarks As String = String.Empty
        Dim sCostCenter As String = String.Empty

        Try
            sFuncName = "AddARInvoice_FHG"

            If CheckBP(sCardName, sCardCode, "C", sErrDesc) <> RTN_SUCCESS Then
                Throw New ArgumentException(sErrDesc)
            End If

            sCostCenter = GetCostCenter(sCardCode)

            If sCostCenter = String.Empty Then sCostCenter = p_oCompDef.sDefaultCostCenter

            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Creating DIAPI ARI Object", sFuncName)
            oDoc = p_oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oInvoices)

            '          Console.WriteLine("Adding A/R Invoice for Customer ::" & sCardName)

            oDoc.CardCode = sCardCode
            oDoc.DocDate = CDate(sBatchPeriod)
            oDoc.DocDueDate = CDate(sBatchPeriod)
            oDoc.TaxDate = CDate(sBatchPeriod)
            oDoc.NumAtCard = "Batch No. : " & sBatchNo
            oDoc.Comments = sBatchPeriod
            oDoc.ImportFileNum = sBatchNo

            For Each row As DataRow In oBPRow
                iCnt += 1
                If iCnt > 1 Then
                    oDoc.Lines.Add()
                End If

                If row.Item(13).ToString = "FFS" Then
                    oDoc.Lines.ItemCode = p_oCompDef.sFFSItemCode
                ElseIf row.Item(13).ToString = "CAP" Then
                    oDoc.Lines.ItemCode = p_oCompDef.sCAPItemCode
                Else
                    sErrDesc = "No Contract Type :: " & row.Item(12).ToString & " found in SAP. Please check the contract type."
                    Throw New ArgumentException(sErrDesc)
                End If

                oDoc.Lines.Quantity = 1
                oDoc.Lines.PriceAfterVAT = CDbl(row.Item(24))

                If bIsNonPanel = True Then
                    oDoc.Lines.VatGroup = "SO"
                Else
                    If CDbl(row.Item(21)) > 0 Then
                        oDoc.Lines.VatGroup = "SO"
                    Else
                        oDoc.Lines.VatGroup = "ZO"
                    End If
                End If

                oDoc.Lines.SerialNum = GetSeriesNum("FVM")
                oDoc.Lines.COGSCostingCode = sCostCenter
                oDoc.Lines.UserFields.Fields.Item("U_AI_PatientName").Value = row.Item(10).ToString
                oDoc.Lines.UserFields.Fields.Item("U_AI_VisitDate").Value = row.Item(1)
                oDoc.Lines.UserFields.Fields.Item("U_AI_VisitNo").Value = row.Item(0).ToString
                oDoc.Lines.UserFields.Fields.Item("U_AI_ICNo").Value = row.Item(11).ToString
                oDoc.Lines.UserFields.Fields.Item("U_AI_ConsultCost").Value = CDbl(row.Item(16))
                oDoc.Lines.UserFields.Fields.Item("U_AI_DrugCost").Value = CDbl(row.Item(17))
                oDoc.Lines.UserFields.Fields.Item("U_AI_InHouseServCost").Value = CDbl(row.Item(18))
                oDoc.Lines.UserFields.Fields.Item("U_AI_ExtServCost").Value = CDbl(row.Item(19))
                oDoc.Lines.UserFields.Fields.Item("U_AI_SubTotal").Value = CDbl(row.Item(20))
                oDoc.Lines.UserFields.Fields.Item("U_AI_GSTAmount").Value = CDbl(row.Item(21))
                oDoc.Lines.UserFields.Fields.Item("U_AI_GrandTotal").Value = CDbl(row.Item(22))
                oDoc.Lines.UserFields.Fields.Item("U_AI_UncliamedAmount").Value = CDbl(row.Item(23))
                oDoc.Lines.UserFields.Fields.Item("U_AI_CostCenter").Value = row.Item(7).ToString
                oDoc.Lines.UserFields.Fields.Item("U_AI_CompanyName").Value = row.Item(2).ToString
                oDoc.Lines.UserFields.Fields.Item("U_AI_ProviderName").Value = row.Item(14).ToString
                oDoc.Lines.UserFields.Fields.Item("U_AI_EmpName").Value = row.Item(8).ToString
                oDoc.Lines.UserFields.Fields.Item("U_AI_EmpID").Value = row.Item(9).ToString

                oDoc.UserFields.Fields.Item("U_AI_BillToDept").Value = row.Item(6).ToString
                oDoc.UserFields.Fields.Item("U_AI_BillToCCenter").Value = row.Item(7).ToString

            Next

           
            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Adding ARI.", sFuncName)
            lRetCode = oDoc.Add
            If lRetCode <> 0 Then
                p_oCompany.GetLastError(lErrCode, sErrDesc)
                If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Adding API failed.", sFuncName)
                Throw New ArgumentException(sErrDesc)
            End If

            Dim iARInvNo As Integer
            p_oCompany.GetNewObjectCode(iARInvNo)
            'AddDataToTable(p_oDtReport, "AR", iARInvNo, sCardCode, String.Empty)

            Console.WriteLine("Successfully Created A/R Invoice ::" & iARInvNo)


            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Fucntion Completed Succesfully.", sFuncName)
            AddARInvoice_FHG = RTN_SUCCESS

        Catch ex As Exception
            AddARInvoice_FHG = RTN_ERROR
            sErrDesc = ex.Message
            WriteToLogFile(sErrDesc, sFuncName)
            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile("Function completed with ERROR:" & sErrDesc, sFuncName)
        End Try
    End Function

End Module
